package com.server.http.service.imlp;

import com.server.dao.UnitDao;
import com.server.dao.MobileAppUnitDao;
import com.server.pojo.MobileApp;
import com.server.pojo.Unit;
import com.server.pojo.MobileAppUnit;
import com.server.libs.ret.RE;
import com.server.http.service.UnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UnitServiceImpl implements UnitService {

    @Autowired
    UnitDao unitDao;

    @Autowired
    MobileAppUnitDao mobileAppUnitDao;

    @Override
    public Unit checkId(Integer id) {
        Unit unit = unitDao.findById(id).orElse(null);
        if (unit == null)throw new RE("该单位不存在");
        return unit;
    }

    @Override
    public Unit checkCode(String code) {
        Unit unit = unitDao.findByCode(code);
        if (unit == null)throw new RE("该单位不存在");
        return unit;
    }

    @Override
    public MobileAppUnit checkJoin(Unit unit, MobileApp mobileApp) {
        MobileAppUnit bind = mobileAppUnitDao.findByMobileAppAndUnit(mobileApp, unit);
        if (bind == null) throw new RE("你没有加入该单位");
        return bind;
    }

    @Override
    public void checkNotJoin(Unit unit, MobileApp mobileApp) {
        MobileAppUnit bind = mobileAppUnitDao.findByMobileAppAndUnit(mobileApp, unit);
        if (bind != null) throw new RE("你已加入该单位");
    }
}
